import { getCategoryAPI } from '@/api/category'
import { computed, onMounted, ref, watch } from 'vue'
import { useRoute } from 'vue-router'

// 封装分类数据业务相关代码
export function useCategory() {
  const categoryData = ref({})
  const route = useRoute()

  const getCategory = async () => {
    const res = await getCategoryAPI(route.params.id)
    categoryData.value = res.data.result
  }
  onMounted(() => {
    getCategory()
  })

  const id = computed(() => {
    return route.params.id
  })

  watch(id, () => {
    getCategory(id)
  })

  return {
    categoryData
  }
}